/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.lswms.modules.job.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lswms.common.constant.Constant;
import com.lswms.common.page.PageData;
import com.lswms.common.service.impl.BaseServiceImpl;
import com.lswms.common.utils.ConvertUtils;
import com.lswms.modules.job.dao.ScheduleJobDao;
import com.lswms.modules.job.dto.ScheduleJobDTO;
import com.lswms.modules.job.entity.ScheduleJobEntity;
import com.lswms.modules.job.service.ScheduleJobService;
import com.lswms.modules.job.utils.ScheduleUtils;

@Service
public class ScheduleJobServiceImpl extends BaseServiceImpl<ScheduleJobDao, ScheduleJobEntity> implements ScheduleJobService {
	@Autowired
	private Scheduler scheduler;

	
	@Override
	public PageData<ScheduleJobDTO> page(Map<String, Object> params) {
		IPage<ScheduleJobEntity> page = baseDao.selectPage(
			getPage(params, Constant.CREATE_DATE, false),
			getWrapper(params)
		);
		return getPageData(page, ScheduleJobDTO.class);
	}

	@Override
	public ScheduleJobDTO get(Long id) {
		ScheduleJobEntity entity = baseDao.selectById(id);

		return ConvertUtils.sourceToTarget(entity, ScheduleJobDTO.class);
	}

	private QueryWrapper<ScheduleJobEntity> getWrapper(Map<String, Object> params){
		String beanName = (String)params.get("beanName");

		QueryWrapper<ScheduleJobEntity> wrapper = new QueryWrapper<>();
		wrapper.like(StringUtils.isNotBlank(beanName), "bean_name", beanName);

		return wrapper;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(ScheduleJobDTO dto) {
		ScheduleJobEntity entity = ConvertUtils.sourceToTarget(dto, ScheduleJobEntity.class);

		entity.setStatus(Constant.ScheduleStatus.NORMAL.getValue());
        this.insert(entity);
        
        ScheduleUtils.createScheduleJob(scheduler, entity);
    }
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(ScheduleJobDTO dto) {
		ScheduleJobEntity entity = ConvertUtils.sourceToTarget(dto, ScheduleJobEntity.class);

        ScheduleUtils.updateScheduleJob(scheduler, entity);
                
        this.updateById(entity);
    }

	@Override
	@Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<ScheduleJobEntity> list) {
		try {
		List<Long> idslist = new ArrayList<Long>() ;
    	for(ScheduleJobEntity id : list){
    		ScheduleUtils.deleteScheduleJob(scheduler, id.getId());
    		idslist.add(id.getId());
    	}
    	
    	//删除数据
    	this.deleteBatchIds(idslist);
		}catch(Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	@Override
    public int updateBatch(Long[] ids, int status){
    	Map<String, Object> map = new HashMap<>(2);
    	map.put("ids", ids);
    	map.put("status", status);
    	return baseDao.updateBatch(map);
    }
    
	@Override
	@Transactional(rollbackFor = Exception.class)
    public void run(List<ScheduleJobEntity> list){
    	for(ScheduleJobEntity id : list){
    		ScheduleUtils.run(scheduler, this.selectById(id.getId()));
    	}
    }

	@Override
	@Transactional(rollbackFor = Exception.class)
    public void pause(List<ScheduleJobEntity> list) {
		List<Long> idslist = new ArrayList<Long>() ;
        for(ScheduleJobEntity id : list){
    		ScheduleUtils.pauseJob(scheduler, id.getId());
    		idslist.add(id.getId());
    	}
        
    	updateBatch( idslist.toArray(new Long[idslist.size()]), Constant.ScheduleStatus.PAUSE.getValue());
    }

	@Override
	@Transactional(rollbackFor = Exception.class)
    public void resume(List<ScheduleJobEntity> list) {
		
		List<Long> idslist = new ArrayList<Long>() ;
    	for(ScheduleJobEntity id : list){
    		ScheduleUtils.resumeJob(scheduler, id.getId());
    		idslist.add(id.getId());
    	}
    	updateBatch( idslist.toArray(new Long[idslist.size()]), Constant.ScheduleStatus.NORMAL.getValue());
    }
    
}